﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using QuickModel.QuickModelFrame;
using Transfer.Utils;

namespace QuickModel.DuctFastModel
{
	[Rebuilder(RebuilderName = "DuctRebuilder")]
	internal class DuctFastModelRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData createdElement)
		{
			createdElement = new BuildData();
			try
			{
				DuctFastModelRequest ductFastModelRequest = inputReqeust as DuctFastModelRequest;
				DuctInfo ductInfo = ductFastModelRequest.DuctInfo;
				bool flag = ductFastModelRequest == null || ductFastModelRequest.DuctInfo == null || ductFastModelRequest.DuctInfo.ParaleCurve == null;
				if (flag)
				{
					return false;
				}
				List<ElementId> list = new List<ElementId>();
				Element element = DuctFormData.m_SysType[DuctFormData.m_strSystemType];
				Element element2 = DuctFormData.m_DuctType[DuctFormData.m_strDuctType];
				XYZ xyz = ductInfo.GetStartEndPoints[0].Add(XYZ.BasisZ * Common.MMToFeet(Convert.ToDouble(DuctFormData.m_strLevel) * 1000.0));
				XYZ xyz2 = ductInfo.GetStartEndPoints[1].Add(XYZ.BasisZ * Common.MMToFeet(Convert.ToDouble(DuctFormData.m_strLevel) * 1000.0));
				xyz = xyz.Add(XYZ.BasisZ * inputDoc.ActiveView.GenLevel.Elevation);
				xyz2 = xyz2.Add(XYZ.BasisZ * inputDoc.ActiveView.GenLevel.Elevation);
				Duct duct = Duct.Create(inputDoc, element.Id, element2.Id, inputDoc.ActiveView.GenLevel.Id, xyz, xyz2);
				list.Add(duct.Id);
				Parameter parameter = duct.get_Parameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
				Parameter parameter2 = duct.get_Parameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
				Parameter parameter3 = duct.get_Parameter(BuiltInParameter.RBS_CURVE_VERT_OFFSET_PARAM);
				Parameter parameter4 = duct.get_Parameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM);
				bool flag2 = false;
				Parameter parameter5 = duct.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM);
				bool flag3 = parameter5 != null;
				if (flag3)
				{
					bool flag4 = parameter5.AsValueString() == "圆形风管";
					if (flag4)
					{
						flag2 = true;
					}
				}
				bool flag5 = parameter != null && !parameter.IsReadOnly;
				if (flag5)
				{
					bool bAppoint = DuctFormData.m_bAppoint;
					if (bAppoint)
					{
						parameter.Set(Common.MMToFeet(DuctFormData.m_dDuctHeight));
					}
					else
					{
						bool bAuto = DuctFormData.m_bAuto;
						if (bAuto)
						{
							bool flag6 = ductInfo.Height != null && Convert.ToDouble(ductInfo.Height.dDuctHeight) > 0.0;
							if (flag6)
							{
								parameter.Set(Common.MMToFeet(Convert.ToDouble(ductInfo.Height.dDuctHeight)));
							}
							else
							{
								parameter.Set(Common.MMToFeet(320.0));
							}
						}
					}
				}
				bool flag7 = parameter3 != null && !parameter3.IsReadOnly;
				if (flag7)
				{
					bool flag8 = DuctFormData.m_strAlignType == "中";
					if (flag8)
					{
						parameter3.Set(0);
					}
					else
					{
						bool flag9 = DuctFormData.m_strAlignType == "底";
						if (flag9)
						{
							parameter3.Set(1);
						}
						else
						{
							parameter3.Set(2);
						}
					}
				}
				bool flag10 = flag2;
				if (flag10)
				{
					bool flag11 = parameter4 != null && !parameter4.IsReadOnly;
					if (flag11)
					{
						parameter4.Set(ductInfo.Width);
					}
				}
				else
				{
					bool flag12 = parameter2 != null && !parameter2.IsReadOnly;
					if (flag12)
					{
						parameter2.Set(ductInfo.Width);
					}
				}
				createdElement.CreatedElement = duct;
				DuctBuildData ductBuildData = new DuctBuildData();
				ductBuildData.NewDuctFastModelRequest = ductFastModelRequest;
				ductBuildData.CreatedElement = duct;
				AllDuctData.DicBuildDatas.Add(ductFastModelRequest.DuctInfo.Index, ductBuildData);
			}
			catch (Exception ex)
			{
				return false;
			}
			return true;
		}
	}
}
